/*     */ package com.zimbra.cs.service.util;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.L10nUtil;
/*     */ import com.zimbra.common.util.L10nUtil.MsgKey;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletContext.Item;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import com.zimbra.cs.service.formatter.Formatter;
/*     */ import com.zimbra.cs.service.formatter.FormatterFactory;
/*     */ import com.zimbra.cs.service.formatter.FormatterFactory.FormatType;
/*     */ import com.zimbra.cs.servlet.util.AuthUtil;
/*     */ import java.io.IOException;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class UserServletUtil
/*     */ {
/*     */   public static void resolveItems(UserServletContext context)
/*     */     throws ServiceException
/*     */   {
/*  53 */     if ((context == null) || (context.params == null)) {
/*  54 */       return;
/*     */     }
/*     */     
/*  57 */     for (UserServletContext.Item item : context.requestedItems) {
/*     */       try {
/*  59 */         if (item.versioned) {
/*  60 */           item.mailItem = context.targetMailbox.getItemRevision(context.opContext, item.id, MailItem.Type.UNKNOWN, item.ver, context.fromDumpster);
/*     */         } else
/*  62 */           item.mailItem = context.targetMailbox.getItemById(context.opContext, item.id, MailItem.Type.UNKNOWN, context.fromDumpster);
/*     */       } catch (MailServiceException.NoSuchItemException x) {
/*  64 */         ZimbraLog.misc.info(x.getMessage());
/*     */       } catch (ServiceException x) {
/*  66 */         if (x.getCode().equals("service.PERM_DENIED")) {
/*  67 */           ZimbraLog.misc.info(x.getMessage());
/*     */         } else {
/*  69 */           throw x;
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static MailItem resolveItem(UserServletContext context, boolean checkExtension)
/*     */     throws ServiceException
/*     */   {
/*  89 */     if ((context.formatter != null) && (!context.formatter.requiresAuth()) && (!FormatterFactory.FormatType.FREE_BUSY.equals(context.formatter.getType())))
/*     */     {
/*  91 */       return null;
/*     */     }
/*     */     
/*  94 */     Mailbox mbox = context.targetMailbox;
/*     */     
/*     */ 
/*  97 */     if (context.imapId > 0)
/*     */     {
/*  99 */       Folder folder = mbox.getFolderByPath(context.opContext, context.itemPath);
/*     */       
/*     */ 
/* 102 */       return mbox.getItemByImapId(context.opContext, context.imapId, folder.getId());
/*     */     }
/*     */     
/* 105 */     if (context.itemId != null) {
/* 106 */       OperationContext opContext = null;
/* 107 */       if ((context.fromDumpster) && (context.isUsingAdminPrivileges()))
/*     */       {
/* 109 */         opContext = new OperationContext(context.authToken);
/*     */       } else {
/* 111 */         opContext = context.opContext;
/*     */       }
/*     */       
/* 114 */       context.target = mbox.getItemById(opContext, context.itemId.getId(), MailItem.Type.UNKNOWN, context.fromDumpster);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/* 119 */       context.itemPath = context.target.getPath();
/* 120 */       if (((context.target instanceof Mountpoint)) || (context.extraPath == null) || (context.extraPath.equals("")))
/* 121 */         return context.target;
/* 122 */       if (context.itemPath == null)
/* 123 */         throw MailServiceException.NO_SUCH_ITEM("?id=" + context.itemId + "&name=" + context.extraPath);
/* 124 */       context.target = null;
/* 125 */       context.itemId = null;
/*     */     }
/*     */     
/* 128 */     if ((context.extraPath != null) && (!context.extraPath.equals(""))) {
/* 129 */       context.itemPath = (context.itemPath + '/' + context.extraPath).replaceAll("//+", "/");
/* 130 */       context.extraPath = null;
/*     */     }
/*     */     
/* 133 */     if ((FormatterFactory.FormatType.FREE_BUSY.equals(context.format)) || (FormatterFactory.FormatType.IFB.equals(context.format)))
/*     */     {
/*     */       try {
/* 136 */         context.target = mbox.getItemByPath(null, context.itemPath);
/*     */       } catch (ServiceException e) {
/* 138 */         if (!(e instanceof MailServiceException.NoSuchItemException)) {
/* 139 */           throw e;
/*     */         }
/*     */       }
/*     */     } else {
/* 143 */       ServiceException failure = null;
/*     */       try {
/* 145 */         context.target = mbox.getItemByPath(context.opContext, context.itemPath);
/*     */       } catch (ServiceException e) {
/* 147 */         if ((!(e instanceof MailServiceException.NoSuchItemException)) && (!e.getCode().equals("service.PERM_DENIED")))
/* 148 */           throw e;
/* 149 */         failure = e;
/*     */       }
/*     */       
/* 152 */       if (context.target == null)
/*     */       {
/*     */ 
/*     */ 
/*     */ 
/*     */         try
/*     */         {
/*     */ 
/*     */ 
/*     */ 
/* 162 */           Pair<Folder, String> match = mbox.getFolderByPathLongestMatch(null, 1, context.itemPath);
/* 163 */           Folder reachable = (Folder)match.getFirst();
/* 164 */           if ((reachable instanceof Mountpoint)) {
/* 165 */             context.target = reachable;
/* 166 */             context.itemPath = reachable.getPath();
/* 167 */             context.extraPath = ((String)match.getSecond());
/*     */           }
/*     */         }
/*     */         catch (ServiceException e) {}
/*     */       }
/* 172 */       if (context.target == null)
/*     */       {
/* 174 */         int dot = context.itemPath.lastIndexOf('.');int slash = context.itemPath.lastIndexOf('/');
/* 175 */         if ((checkExtension) && (context.format == null) && (dot != -1) && (dot > slash))
/*     */         {
/*     */ 
/*     */ 
/* 179 */           String unsuffixedPath = context.itemPath.substring(0, dot);
/*     */           try {
/* 181 */             context.format = FormatterFactory.FormatType.fromString(context.itemPath.substring(dot + 1));
/* 182 */             if (context.format != null) {
/* 183 */               context.formatter = ((Formatter)FormatterFactory.mFormatters.get(context.format));
/*     */             }
/* 185 */             if ((context.formatter != null) && (!context.formatter.requiresAuth()))
/*     */             {
/* 187 */               context.target = mbox.getItemByPath(null, unsuffixedPath);
/*     */             } else {
/* 189 */               context.target = mbox.getItemByPath(context.opContext, unsuffixedPath);
/*     */             }
/* 191 */             context.itemPath = unsuffixedPath;
/*     */           }
/*     */           catch (ServiceException e) {}
/*     */         }
/*     */       }
/* 196 */       if (context.target == null) {
/* 197 */         throw failure;
/*     */       }
/*     */     }
/* 200 */     return context.target;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Mailbox getTargetMailbox(UserServletContext context)
/*     */   {
/* 217 */     Mailbox mbox = null;
/*     */     try {
/* 219 */       mbox = context.targetMailbox = MailboxManager.getInstance().getMailboxByAccount(context.targetAccount);
/*     */     }
/*     */     catch (Exception e) {}
/*     */     
/*     */ 
/* 224 */     return mbox;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void getAccount(UserServletContext context)
/*     */     throws IOException, ServletException, UserServletException
/*     */   {
/*     */     try
/*     */     {
/* 239 */       boolean isAdminRequest = AuthUtil.isAdminRequest(context.req);
/*     */       
/*     */ 
/* 242 */       if ((context.cookieAuthAllowed()) || (AuthProvider.allowAccessKeyAuth(context.req, context.getServlet()))) {
/*     */         try {
/* 244 */           AuthToken at = AuthProvider.getAuthToken(context.req, isAdminRequest);
/* 245 */           if (at != null)
/*     */           {
/* 247 */             if (at.isZimbraUser()) {
/* 248 */               if (!at.isRegistered()) {
/* 249 */                 throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */               }
/*     */               try
/*     */               {
/* 253 */                 context.setAuthAccount(AuthProvider.validateAuthToken(Provisioning.getInstance(), at, false));
/*     */               } catch (ServiceException e) {
/* 255 */                 throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */               }
/*     */               
/* 258 */               context.cookieAuthHappened = true;
/* 259 */               context.authToken = at;
/* 260 */               return;
/*     */             }
/* 262 */             if (at.isExpired()) {
/* 263 */               throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */             }
/*     */             
/* 266 */             context.setAuthAccount(new GuestAccount(at));
/* 267 */             context.basicAuthHappened = true;
/* 268 */             context.authToken = at;
/* 269 */             return;
/*     */           }
/*     */         }
/*     */         catch (AuthTokenException e)
/*     */         {
/* 274 */           throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/* 280 */       if (context.queryParamAuthAllowed()) {
/* 281 */         String auth = (String)context.params.get("zauthtoken");
/* 282 */         if (auth == null)
/* 283 */           auth = (String)context.params.get("authToken");
/* 284 */         if (auth != null) {
/*     */           try
/*     */           {
/* 287 */             AuthToken at = AuthProvider.getAuthToken(auth);
/*     */             try
/*     */             {
/* 290 */               context.setAuthAccount(AuthProvider.validateAuthToken(Provisioning.getInstance(), at, false));
/* 291 */               context.qpAuthHappened = true;
/* 292 */               context.authToken = at;
/* 293 */               return;
/*     */             } catch (ServiceException e) {
/* 295 */               throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */             }
/*     */             
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 313 */             if (!context.basicAuthAllowed()) {
/*     */               return;
/*     */             }
/*     */           }
/*     */           catch (AuthTokenException e)
/*     */           {
/* 301 */             throw new UserServletException(401, L10nUtil.getMessage(L10nUtil.MsgKey.errMustAuthenticate, context.req, new Object[0]));
/*     */           }
/*     */         }
/*     */       }
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 314 */       context.setAuthAccount(AuthUtil.basicAuthRequest(context.req, context.resp, context.servlet, false));
/* 315 */       if (context.getAuthAccount() != null) {
/* 316 */         context.basicAuthHappened = true;
/* 317 */         context.authToken = AuthProvider.getAuthToken(context.getAuthAccount(), isAdminRequest);
/*     */         
/*     */ 
/* 320 */         if (context.setCookie()) {
/* 321 */           boolean secureCookie = context.req.getScheme().equals("https");
/* 322 */           context.authToken.encode(context.resp, isAdminRequest, secureCookie);
/*     */         }
/*     */       }
/*     */       
/* 326 */       return;
/*     */ 
/*     */     }
/*     */     catch (ServiceException e)
/*     */     {
/* 331 */       throw new ServletException(e);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/util/UserServletUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */